package com.djzhou.gmms.base.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.djzhou.gmms.model.company.TbCompany;
import com.djzhou.gmms.model.department.TbDepartment;
import com.djzhou.gmms.model.entity.Entity;
import com.djzhou.gmms.model.entity.PageBean;
import com.djzhou.gmms.model.menu.TbMenu;
import com.djzhou.gmms.model.role.TbRole;
import com.djzhou.gmms.model.systeminfo.TbSystemInfo;
import com.djzhou.gmms.model.user.TbUser;
import com.djzhou.gmms.model.user.VoResource;
import com.djzhou.gmms.service.announcement.AnnouncementService;
import com.djzhou.gmms.service.authority.AuthorityService;
import com.djzhou.gmms.service.backlog.BacklogService;
import com.djzhou.gmms.service.company.CompanyService;
import com.djzhou.gmms.service.department.DepartmentService;
import com.djzhou.gmms.service.dictionary.DictionaryService;
import com.djzhou.gmms.service.dictionary.DictionaryTypeService;
import com.djzhou.gmms.service.email.AttachmentService;
import com.djzhou.gmms.service.email.EmailService;
import com.djzhou.gmms.service.events.EventsService;
import com.djzhou.gmms.service.log.LogService;
import com.djzhou.gmms.service.menu.MenuAuthorityService;
import com.djzhou.gmms.service.menu.MenuService;
import com.djzhou.gmms.service.menu.UserMenuConfigService;
import com.djzhou.gmms.service.message.MessageService;
import com.djzhou.gmms.service.position.PositionService;
import com.djzhou.gmms.service.role.RoleAuthorityService;
import com.djzhou.gmms.service.role.RoleService;
import com.djzhou.gmms.service.systeminfo.SystemInfoService;
import com.djzhou.gmms.service.updatelog.UpdateLogService;
import com.djzhou.gmms.service.user.UserDepartmentService;
import com.djzhou.gmms.service.user.UserRoleService;
import com.djzhou.gmms.service.user.UserService;
import com.djzhou.gmms.service.warning.WarningService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * web层基类
 * @author DJZHOU
 *
 * @param <T>
 */
public abstract class BaseAction<T extends Entity> extends ActionSupport implements Serializable {
	protected Integer loadData;
	
	protected String listUrl;
	
	protected List<TbMenu> historyMenuList;
	
	private static final long serialVersionUID = 9041390863034431674L;
	
	/**
	 * 每页记录数 EXTJS专用变量
	 */
	protected int limit=querySystemInfo()!=null?querySystemInfo().getPagesum():10;
	
	protected Integer keyId;//主键ID
	protected T obj;//对象
	protected List<T> list;//集合
	protected String auditing;//正在审核 用于区分普通列表和审核列表
	
	/**
	 * 当前页
	 */
	protected int currentPage = 1;
	
	/**
	 * 每页记录数
	 */
	protected int pageSize = querySystemInfo()!=null?querySystemInfo().getPagesum():10;
	
	protected int totalPage=1;
	
	protected int totalCount=0;
	
	/**
	 * 当前菜单ID
	 */
	protected int currentMenuId=0;
	/**
	 * 当前菜单名称
	 */
	protected String currentMenuName;
	/**
	 * 父级菜单名称
	 */
	protected String parentMenuName;
	/**
	 * 根据当前菜单所查到的权限编码字符串
	 */
	protected String authorityCode;
	
	/**
	 * 封装的分页信息
	 */
	protected PageBean<T> pageBean=new PageBean<T>();
	/**
	 * 返回是否成功标识 1成功 0失败 N其他情况
	 */
	protected Integer successFlag;
	/**
	 * 直接返回消息
	 */
	protected String msg="数据保存失败,请稍后再试或联系管理员.";
	/**
	 * EXT表单提交用返回数据实体
	 */
	protected ExtMessage ajaxMsg=new ExtMessage();
	
	/**
	 * 子集部门ID串
	 */
	protected String childDepartmentIds;
	/**
	 * 子集公司ID串
	 */
	protected String childCompanyIds;
	
	/**
	 * 所有下级部门ID串
	 */
	protected String allChildDepartmentIds;
	/**
	 * 所有后代公司ID串
	 */
	protected String allChildCompanyIds;
	/**
	 * 当前主角色
	 */
	protected Integer currentMainRoleId;
	
	
	protected String sidx;//排序列
	protected String sord;//排序方式
	protected Integer rows=pageSize;//每页显示数
	protected String searchStr;//自动查询控件所需模糊字符串
	protected VoResource resource;//自动查询控件所需数据源
	/**
	 * 模糊查询组件与EXTJS公用变量
	 */
	protected int page=1;//当前页数
	
	protected AuthorityService authorityService;
	protected CompanyService companyService;
	protected DepartmentService departmentService;
	protected DictionaryService dictionaryService;
	protected DictionaryTypeService dictionaryTypeService;
	protected LogService logService;
	protected MenuService menuService;
	protected MenuAuthorityService menuAuthorityService;
	protected PositionService positionService;
	protected RoleService roleService;
	protected RoleAuthorityService roleAuthorityService;
	protected SystemInfoService systemInfoService;
	protected UserService userService;
	protected UserRoleService userRoleService;
	protected UserDepartmentService userDepartmentService;
	protected AnnouncementService announcementService;
	protected EmailService emailService;
	protected AttachmentService attachmentService;
	protected MessageService messageService;
	protected UserMenuConfigService userMenuConfigService;
	protected EventsService eventsService;
	protected BacklogService backlogService;
	protected WarningService warningService;
	protected UpdateLogService updateLogService;
	
	/*****TASK扩展代码开始*******/
	//protected AreaService taskAreaService;
	//public AreaService getTaskAreaService() {
	//	return taskAreaService;
	//}
	//public void setTaskAreaService(AreaService taskAreaService) {
	//	this.taskAreaService = taskAreaService;
	//}


	//
//
	/**
	 * 查询当前登录用户的办事处(区域)
	 *FOR JEasyFrame
	 * @return
	 */
	//public TaskArea queryCurrentArea() {
	//	Integer areaId=((TbUser)ServletActionContext.getRequest().getSession().getAttribute("sessionUser")).getAreaId();
	//	area.setAreaId(0);
	//	area.setAreaName("无区域");
	//	return area;
	//}
	/*******TASK扩展代码结束***************/
	
	
	/**
	 * 查询session中的公司
	 *FOR JEasyFrame
	 * @return
	 */
	public TbCompany queryCompany() {
		return ((TbUser)ServletActionContext.getRequest().getSession().getAttribute("sessionUser")).getCompany();
	}
	/**
	 * 查询session中的部门
	 *FOR JEasyFrame
	 * @return
	 */
	public TbDepartment queryDepartment() {
		return ((TbUser)ServletActionContext.getRequest().getSession().getAttribute("sessionUser")).getDepartment();
	}
	/**
	 * 查询session中的用户
	 *FOR JEasyFrame
	 * @return
	 */
	public TbUser queryUser() {
		return (TbUser)ServletActionContext.getRequest().getSession().getAttribute("sessionUser");
	}
	/**
	 * 获取用户主角色
	 *FOR JEasyFrame
	 * @return
	 */
	public TbRole queryMainRole(){
		return userRoleService.queryMainRoleByUserId(queryUser().getUserId());
	}
	
	/**
	 * 查询session中的角色ID串,jsp中使用
	 *FOR JEasyFrame
	 * @return
	 */
	public String queryRoleIds4Page() {
		return (String)ServletActionContext.getRequest().getSession().getAttribute("sessionRoleIds");
	}
	/**
	 * 查询session中的角色ID串,java中使用
	 *FOR JEasyFrame
	 * @return
	 */
	public String queryRoleIds4Java() {
		String str=(String)ServletActionContext.getRequest().getSession().getAttribute("sessionRoleIds");
		if(str!=null){
			return str.substring(1,str.length()-1);
		}else{
			return "";
		}
		
		
	}
	/**
	 * 查询session中的角色列表
	 *FOR JEasyFrame
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TbRole> queryRoleList() {
		return (List<TbRole>)ServletActionContext.getRequest().getSession().getAttribute("sessionRoleList");
	}
	
	public TbSystemInfo querySystemInfo() {
		return (TbSystemInfo)ServletActionContext.getRequest().getSession().getAttribute("systemInfo");
	}
	
	/**
	 * 查询集合 包括模糊查询
	 * @return
	 *
	 */
	public String queryList(){
		return SUCCESS;
	}
	/**
	 * 查询集合 包括模糊查询 为EXTJS专用
	 * @return
	 *
	 */
	public String queryList4ExtJs(){
		return SUCCESS;
	}
	/**
	 * 跳转添加记录页面
	 * @return
	 *
	 */
	public String toAdd(){
		return SUCCESS;
	}
	/**
	 * 跳转编辑记录页面
	 * @return
	 *
	 */
	public String toEdit(){
		setCurrentPage(currentPage);
		return SUCCESS;
	}
	/**
	 * 保存数据
	 * @return
	 *
	 */
	public String save(){
		setCurrentPage(currentPage);
		return SUCCESS;
	}
	/**
	 * 修改数据
	 * @return
	 *
	 */
	public String modify(){
		setCurrentPage(currentPage);
		return SUCCESS;
	}
	/**
	 * 删除数据
	 * @return
	 *
	 */
	public String delete(){
		setCurrentPage(currentPage);
		return SUCCESS;
	}
	
	/******************SET GET方法*************************/
	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public PageBean<T> getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean<T> pageBean) {
		this.pageBean = pageBean;
	}

	public AuthorityService getAuthorityService() {
		return authorityService;
	}

	public void setAuthorityService(AuthorityService authorityService) {
		this.authorityService = authorityService;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public DictionaryService getDictionaryService() {
		return dictionaryService;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	public DictionaryTypeService getDictionaryTypeService() {
		return dictionaryTypeService;
	}

	public void setDictionaryTypeService(DictionaryTypeService dictionaryTypeService) {
		this.dictionaryTypeService = dictionaryTypeService;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public PositionService getPositionService() {
		return positionService;
	}

	public void setPositionService(PositionService positionService) {
		this.positionService = positionService;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public RoleAuthorityService getRoleAuthorityService() {
		return roleAuthorityService;
	}

	public void setRoleAuthorityService(RoleAuthorityService roleAuthorityService) {
		this.roleAuthorityService = roleAuthorityService;
	}

	public SystemInfoService getSystemInfoService() {
		return systemInfoService;
	}

	public void setSystemInfoService(SystemInfoService systemInfoService) {
		this.systemInfoService = systemInfoService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserRoleService getUserRoleService() {
		return userRoleService;
	}

	public void setUserRoleService(UserRoleService userRoleService) {
		this.userRoleService = userRoleService;
	}

	public UserDepartmentService getUserDepartmentService() {
		return userDepartmentService;
	}

	public void setUserDepartmentService(UserDepartmentService userDepartmentService) {
		this.userDepartmentService = userDepartmentService;
	}
	
	public WarningService getWarningService() {
		return warningService;
	}
	public void setWarningService(WarningService warningService) {
		this.warningService = warningService;
	}
	public Integer getSuccessFlag() {
		return successFlag;
	}

	public void setSuccessFlag(Integer successFlag) {
		this.successFlag = successFlag;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public MenuAuthorityService getMenuAuthorityService() {
		return menuAuthorityService;
	}

	public void setMenuAuthorityService(MenuAuthorityService menuAuthorityService) {
		this.menuAuthorityService = menuAuthorityService;
	}
	

	public EventsService getEventsService() {
		return eventsService;
	}

	public void setEventsService(EventsService eventsService) {
		this.eventsService = eventsService;
	}

	public int getCurrentMenuId() {
		return currentMenuId;
	}

	public void setCurrentMenuId(int currentMenuId) {
		this.currentMenuId = currentMenuId;
	}

	public String getAuthorityCode() {
		authorityCode=roleAuthorityService.queryAuthorityCodeByMenuAndRole(queryRoleIds4Java(), currentMenuId);
		return authorityCode;
	}

	public void setAuthorityCode(String authorityCode) {
		this.authorityCode = authorityCode;
	}

	public AnnouncementService getAnnouncementService() {
		return announcementService;
	}

	public void setAnnouncementService(AnnouncementService announcementService) {
		this.announcementService = announcementService;
	}

	public EmailService getEmailService() {
		return emailService;
	}

	public void setEmailService(EmailService emailService) {
		this.emailService = emailService;
	}

	public AttachmentService getAttachmentService() {
		return attachmentService;
	}

	public void setAttachmentService(AttachmentService attachmentService) {
		this.attachmentService = attachmentService;
	}

	public MessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public UserMenuConfigService getUserMenuConfigService() {
		return userMenuConfigService;
	}

	public void setUserMenuConfigService(UserMenuConfigService userMenuConfigService) {
		this.userMenuConfigService = userMenuConfigService;
	}

	public BacklogService getBacklogService() {
		return backlogService;
	}
	public void setBacklogService(BacklogService backlogService) {
		this.backlogService = backlogService;
	}
	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public String getSearchStr() {
		return searchStr;
	}

	public void setSearchStr(String searchStr) {
		this.searchStr = searchStr;
	}

	public VoResource getResource() {
		return resource;
	}

	public void setResource(VoResource resource) {
		this.resource = resource;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}
	/**
	 * 获取下级部门ID串
	 * @return
	 */
	public String getChildDepartmentIds() {
		childDepartmentIds="";
		List<TbDepartment> departments=departmentService.queryChildList(queryCompany().getCompanyId(), queryDepartment().getDepartmentId());
		for(TbDepartment dep:departments){
			childDepartmentIds+=dep.getDepartmentId()+",";
		}
		return childDepartmentIds;
	}

	public void setChildDepartmentIds(String childDepartmentIds) {
		this.childDepartmentIds = childDepartmentIds;
	}
	/**
	 * 获取下级公司ID串
	 * @return
	 */
	public String getChildCompanyIds() {
		childCompanyIds="";
		List<TbCompany> companies=companyService.queryChildList(queryCompany().getCompanyId());
		for(TbCompany com:companies){
			childCompanyIds+=com.getCompanyId()+",";
		}
		return childCompanyIds;
	}
	

	public void setChildCompanyIds(String childCompanyIds) {
		this.childCompanyIds = childCompanyIds;
	}
	
	/**
	 * 获取所有子集公司ID串
	 * @return
	 */
	public String getAllChildDepartmentIds() {
		allChildDepartmentIds="";
		List<TbDepartment> departments=departmentService.queryAllChildList(queryCompany().getCompanyId(), queryDepartment().getDepartmentId());
		for(TbDepartment dep:departments){
			allChildDepartmentIds+=dep.getDepartmentId()+",";
		}
		return allChildDepartmentIds;
	}

	public void setAllChildDepartmentIds(String allChildDepartmentIds) {
		this.allChildDepartmentIds = allChildDepartmentIds;
	}
	/**
	 * 获取所有子集部门ID串
	 * @return
	 */
	public String getAllChildCompanyIds() {
		allChildCompanyIds="";
		List<TbCompany> companies=companyService.queryAllChildList(queryCompany().getCompanyId());
		for(TbCompany com:companies){
			allChildCompanyIds+=com.getCompanyId()+",";
		}
		return allChildCompanyIds;
	}

	public void setAllChildCompanyIds(String allChildCompanyIds) {
		this.allChildCompanyIds = allChildCompanyIds;
	}


	public class ExtMessage{
		private boolean success;
		private String data;
		public boolean isSuccess() {
			return success;
		}
		public void setSuccess(boolean success) {
			this.success = success;
		}
		public String getData() {
			return data;
		}
		public void setData(String data) {
			this.data = data;
		}
		
	}


	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public ExtMessage getAjaxMsg() {
		return ajaxMsg;
	}

	public void setAjaxMsg(ExtMessage ajaxMsg) {
		this.ajaxMsg = ajaxMsg;
	}

	public void setPage(int page) {
		this.page = page;
	}
	@SuppressWarnings("unchecked")
	public String getCurrentMenuName() {
		if(currentMenuId!=0){
			currentMenuName=menuService.queryById(currentMenuId).getMenuName();
		}else{
			currentMenuName="菜单加载失败";
		}
		historyMenuList=(List<TbMenu>)ServletActionContext.getRequest().getSession().getAttribute("historyMenuList");
		if(historyMenuList==null)historyMenuList=new ArrayList<TbMenu>();
		if(currentMenuId!=0){
			TbMenu menu=menuService.queryById(currentMenuId);
			boolean flag=true;
			for(TbMenu m:historyMenuList){
				if(m.getMenuId()==currentMenuId || m.getParentMenuId()==0){
					flag=false;
					break;
				}
			}
			if(flag){
				if(historyMenuList.size()>=10){
					historyMenuList.remove(0);
				}
				historyMenuList.add(menu);
				ServletActionContext.getRequest().getSession().setAttribute("historyMenuList", historyMenuList);
			}
		}
		return currentMenuName;
	}
	public void setCurrentMenuName(String currentMenuName) {
		this.currentMenuName = currentMenuName;
	}
	public String getParentMenuName() {
		if(currentMenuId!=0){
			TbMenu tempMenu=menuService.queryById(currentMenuId);
			parentMenuName=menuService.queryById(tempMenu.getParentMenuId()).getMenuName();
		}else{
			parentMenuName="菜单加载失败";
		}
		return parentMenuName;
	}
	public void setParentMenuName(String parentMenuName) {
		this.parentMenuName = parentMenuName;
	}
	
	/**
	 * 车辆轮胎限制：头车10个，挂车12个，备胎1个
	 */
	public int getRunningTyreSum(){
		return 10;
	}
	public int getHangRunningTyreSum(){
		return 12;
	}
	public int getSpareTyreSum(){
		return 1;
	}
	/**
	 * EXT返回消息
	 * @param messageInfo
	 */
	protected void setReturnMsg(String messageInfo){
		msg=messageInfo;//普通页面用ajax返回信息
		
		//EXT表单AJAX提交返回用实体
		ajaxMsg=new ExtMessage();
		ajaxMsg.setSuccess(true);
		ajaxMsg.setData(msg);
	}
	public List<TbMenu> getHistoryMenuList() {
		return historyMenuList;
	}
	public void setHistoryMenuList(List<TbMenu> historyMenuList) {
		this.historyMenuList = historyMenuList;
	}
	public Integer getLoadData() {
		return loadData;
	}
	public void setLoadData(Integer loadData) {
		this.loadData = loadData;
	}
	public String getListUrl() {
		return listUrl;
	}
	public void setListUrl(String listUrl) {
		this.listUrl = listUrl;
	}
	public int getCurrentUserId(){
		return queryUser().getUserId();
	}
	public int getCurrentRoleId() {
		return queryMainRole().getRoleId();
	}
	public Integer getCurrentMainRoleId() {
		if(currentMainRoleId==null)
			setCurrentMainRoleId(queryMainRole().getRoleId());
		return currentMainRoleId;
	}
	public Date getCurrentDate(){
		return new Date();
	}
	public void setCurrentMainRoleId(Integer currentMainRoleId) {
		this.currentMainRoleId = currentMainRoleId;
	}
	public UpdateLogService getUpdateLogService() {
		return updateLogService;
	}
	public void setUpdateLogService(UpdateLogService updateLogService) {
		this.updateLogService = updateLogService;
	}
	protected PageBean<Object[]> convertListToPageBean(List<Object[]> list) {
		//把分页信息保存到PageBean中
		PageBean<Object[]> pageBean = new PageBean<Object[]>();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(PageBean.countCurrentPage(currentPage)); 
		pageBean.setAllRow(list.size()); 
		pageBean.setTotalPage(PageBean.countTotalPage(pageSize, pageBean.getAllRow()));
		List<Object[]> source = new ArrayList<Object[]>();
		for(int i=0;i<pageSize;i++){
			int index = i+(pageBean.getCurrentPage()-1)*pageSize;
			if(index>=pageBean.getAllRow())//超出
				break;
			source.add(list.get(index));
		}
		pageBean.setList(source); 
		totalCount=pageBean.getAllRow();
		totalPage=pageBean.getTotalPage();
		pageBean.init();
		return pageBean;
	}
	public Integer getKeyId() {
		return keyId;
	}
	public void setKeyId(Integer keyId) {
		this.keyId = keyId;
	}
	public T getObj() {
		return obj;
	}
	public void setObj(T obj) {
		this.obj = obj;
	}
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	public String getAuditing() {
		return auditing;
	}
	public void setAuditing(String auditing) {
		this.auditing = auditing;
	}
	

}
